package cn.springboot.linked;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import cn.springboot.entity.Category;

/**
 * @author liufl E-mail:Huberier@allpyra.com
 * @version 创建时间：2017年9月27日 下午4:34:37
 * Combiner阶段：将相同key值的词频累加获得词频
 */
public class InvertedIndexCombiner extends Reducer<Text, Text, Text, Text>{

    private Text info = new Text();

    @Override
    protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context)
            throws IOException, InterruptedException {

        //统计词频
        int sum = 0;
        for (Text value : values) {
            sum += Integer.parseInt(value.toString() );
        }

        int splitIndex = key.toString().indexOf(";");

        //重新设置value值由URI和词频组成
        info.set( key.toString().substring( splitIndex + 1) +":"+sum );

        //重新设置key值为单词
        key.set( key.toString().substring(0,splitIndex));

        context.write(key, info);
    }
    
    public static void main(String[] args) {
    	//传值
		int i = 1;
		int j=i;
		j=2;
		System.out.println(i);
		
		//传对象
		Category art = new Category();
		art.setName("张三");
		Category cat =art;
		cat.setName("李四");
		System.out.println(art.getName());
		
		List<List<String>> parent = new ArrayList<List<String>>();
		List<String> child = new ArrayList<>();
		child.add("before");
		parent.add(child);
		child.add("after");
		System.out.println(parent.toString());
		
		String a = "hello";
		String b = "world";
		change(a,b);
		StringBuffer sba = new StringBuffer("hello");
		StringBuffer sbb = new StringBuffer("world");
		change(sba,sbb);
		System.out.println("a:"+a+";;b:"+b+";;sba:"+sba+";;sbb:"+sbb);
		
		Set<Integer> set1 = new HashSet<Integer>() {
			private static final long serialVersionUID = 1L;
			{
			add(1);
			add(3);
			add(5);
			}
		};
		System.out.println(Arrays.asList(set1).toString());
		Integer[] newArr = set1.toArray(new Integer[set1.size()]);
		System.out.println(Arrays.asList(newArr).toString());
	}

	private static void change(StringBuffer sba, StringBuffer sbb) {
		sba = sbb;
		sbb.append(sba);
		Thread t = new Thread(){
			@Override
			public void run() {
				System.out.println("1111111");
			}
		};
		t.start();
	}

	private static void change(String a, String b) {
		a=b;
		b=a+b;
	}
}